package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.ShopBaseController;
import com.yunc.base.utils.R;
import com.yunc.modules.shop.entity.ShopMember;
import com.yunc.modules.shop.service.IShopMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 用户管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:30
 */
@RestController
@RequestMapping("shopMember")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopMemberController extends ShopBaseController {

    private final IShopMemberService shopMemberService;
    
    @GetMapping("/page")
    public R page(Page<ShopMember> page, ShopMember member){
        QueryWrapper<ShopMember> qw = new QueryWrapper<>();
        setShop(qw);
        shopMemberService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ShopMember member){
        QueryWrapper<ShopMember> qw = new QueryWrapper<>();
        setShop(qw);
        List<ShopMember> list = shopMemberService.list(qw);
        return R.ok().put("list", list).put("member", member);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        ShopMember member = shopMemberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ShopMember member){
        member.setCreateTime(new Date());
        boolean flag = shopMemberService.save(member);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(@RequestBody ShopMember member){
        boolean flag = shopMemberService.updateById(member);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
